package mammuthus.web.action

import org.springframework.web.bind.annotation.{RequestMethod, RequestMapping}
import javax.servlet.http.HttpServletRequest
import java.util
import scala.collection.JavaConversions._


/**
 * 6/7/15 WilliamZhu(allwefantasy@gmail.com)
 */

trait HbaseAction {
  this: AppAction =>

  @RequestMapping(Array("startHbaseMaster.html"))
  def startHbaseMaster(req: HttpServletRequest) = {
    val params = putToReqAttribute(req)
    val res = slaveService.startHbaseMaster(params("appName"),params.getOrElse("action","start"))

    nav2(req,params, res,
      prePath = (s"#", "启动Master"),
      nextPath = (s"/app/startHbaseSlaves.html?appName=${params("appName")}", "启动Slaves")
    )

    "shellCommandExecuteResult"
  }

  @RequestMapping(value = Array("addHbaseSlaves.html"), method = Array(RequestMethod.POST))
  def addHbaseSlavesWithPost(req: HttpServletRequest) = {
    val params = putToReqAttribute(req)
    val appName = params("appName")
    val res = slaveService.addHbaseSlaves(params("appName"),params("slave")).toList

    nav(req, res,
      nextPath = (s"/app/startHbaseSlaves.html?appName=${appName}&nextPath=-&prePath=-", "启动新增data node")
    )

    "multiShellCommandExecuteResult"
  }


  @RequestMapping(value = Array("removeHbaseSlaves.html"), method = Array(RequestMethod.POST))
  def removeHbaseSlavesWithPost(req: HttpServletRequest) = {
    val params = putToReqAttribute(req)
    val appName = params("appName")
    val slaves = params("slave")

    val ress = new scala.collection.mutable.ArrayBuffer[Map[String,AnyRef]]()

    val res = slaveService.startSpecificHbaseSlaves(appName,slaves)

    ress += Map("name"->"","rs"->res)

    val dataNodes = slaveService.getAPPExtra(appName,"dataNodes").split(",").toSet
    val newDataNodes = dataNodes -- slaves.split(",").toSet
    val res2 = slaveService.putAPPExtra(appName,Map("dataNodes" -> newDataNodes.mkString(",")))
    ress += Map("name"->"","rs"->res2)

    nav(req, ress.toList,
      nextPath = (s"#", "-")
    )
    "multiShellCommandExecuteResult"
  }


  @RequestMapping(Array("startHbaseSlaves.html"))
  def startHbaseSlaves(req: HttpServletRequest) = {
    val params = putToReqAttribute(req)
    val res = slaveService.startHbaseSlaves(params("appName"),params.getOrElse("action","start"))
    nav2(req,params, res,
      prePath = (s"#", "#"),
      nextPath = (s"#", "完成")
    )

    "shellCommandExecuteResult"
  }

  def hbaseConfigure(req: HttpServletRequest, app: String, step: String, slave: Array[String]): Unit = {
    if (app != "hbase") return
    val stepName = if (step.isEmpty) {
      req.setAttribute("step", "next")
      "Name node 节点选取"
    } else if (step == "next") {
      req.setAttribute("step", "finish")
      val iterKVs = new util.HashMap[String, String]()
      iterKVs.put("nameNodes", slave.mkString(","))
      req.setAttribute("iterKVs", iterKVs)
      "Data node 节点选取"
    } else {
      val iterKVs = new util.HashMap[String, String]()
      iterKVs.put("nameNodes", req.getParameter("nameNodes"))
      iterKVs.put("dataNodes", slave.mkString(","))
      iterKVs.put("slave", (req.getParameter("nameNodes").split(",").toSet ++ slave.toSet).mkString(","))
      req.setAttribute("iterKVs", iterKVs)
      val zks:util.List[String] = slaveService.findAppNamesByApp("zookeeper").split(",").toList
      req.setAttribute("zks",zks)
      val hadoops:util.List[String] = slaveService.findAppNamesByApp("hadoop").split(",").toList
      req.setAttribute("hadoops",hadoops)
      ""
    }
    req.setAttribute("configureType",app)
    req.setAttribute("stepName", stepName)
  }




}
